/*=============================================================================

  Library: CFS

  Copyright (c) German Cancer Research Center,
    Division of Medical and Biological Informatics

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

=============================================================================*/


#include "CFSConfigurationEvent.h"

#include <CFSException.h>

class CFSConfigurationEventData : public QSharedData
{
public:

  CFSConfigurationEventData(const CFSServiceReference& reference,
                            CFSConfigurationEvent::Type type,
                            const QString& factoryPid,
                            const QString& pid)
    : type(type), factoryPid(factoryPid), pid(pid), reference(reference)
  {

  }

  CFSConfigurationEventData(const CFSConfigurationEventData& other)
    : QSharedData(other), type(other.type), factoryPid(other.factoryPid),
      pid(other.pid), reference(other.reference)
  {

  }

  /**
   * Type of this event.
   */
  const CFSConfigurationEvent::Type type;

  /**
   * The factory pid associated with this event.
   */
  const QString factoryPid;

  /**
   * The pid associated with this event.
   */
  const QString pid;

  /**
   * The ConfigurationAdmin service which created this event.
   */
  const CFSServiceReference reference;
};

//----------------------------------------------------------------------------
CFSConfigurationEvent::CFSConfigurationEvent()
  : d(0)
{

}

//----------------------------------------------------------------------------
CFSConfigurationEvent::~CFSConfigurationEvent()
{

}

//----------------------------------------------------------------------------
bool CFSConfigurationEvent::isNull() const
{
  return !d;
}

//----------------------------------------------------------------------------
CFSConfigurationEvent::CFSConfigurationEvent(const CFSServiceReference& reference,
                      Type type, const QString& factoryPid,
                      const QString& pid)
  : d(new CFSConfigurationEventData(reference, type, factoryPid, pid))
{
  if (pid.isNull())
  {
    throw CFSInvalidArgumentException("pid must not be null");
  }
}

//----------------------------------------------------------------------------
CFSConfigurationEvent::CFSConfigurationEvent(const CFSConfigurationEvent& other)
  : d(other.d)
{

}

//----------------------------------------------------------------------------
CFSConfigurationEvent& CFSConfigurationEvent::operator=(const CFSConfigurationEvent& other)
{
  d = other.d;
  return *this;
}

//----------------------------------------------------------------------------
QString CFSConfigurationEvent::getFactoryPid() const
{
  return d->factoryPid;
}

//----------------------------------------------------------------------------
QString CFSConfigurationEvent::getPid() const
{
  return d->pid;
}

//----------------------------------------------------------------------------
int CFSConfigurationEvent::getType() const
{
  return d->type;
}

//----------------------------------------------------------------------------
CFSServiceReference CFSConfigurationEvent::getReference() const
{
  return d->reference;
}

//----------------------------------------------------------------------------
QDebug operator<<(QDebug dbg, CFSConfigurationEvent::Type type)
{
  switch (type)
  {
  case CFSConfigurationEvent::CM_UPDATED: return dbg << "CM_UPDATED";
  case CFSConfigurationEvent::CM_DELETED: return dbg << "CM_DELETED";

  default: return dbg << "unknown configuration event type (" << static_cast<int>(type) << ")";
  }
}

//----------------------------------------------------------------------------
QDebug operator<<(QDebug dbg, const CFSConfigurationEvent& event)
{
  if (event.isNull()) return dbg << "NONE";

  dbg.nospace() << event.getType() << (event.getFactoryPid().isEmpty() ? " " : event.getFactoryPid() + ", ")
                   << "pid=" << event.getPid();
  return dbg.maybeSpace();
}
